package com.itstyle.jpa.service.impl;

import com.itstyle.jpa.model.User;
import com.itstyle.jpa.repository.UserRepository;
import com.itstyle.jpa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.util.List;

/**
 * @name: UserServiceImpl
 * @Author: wangshimin
 * @Date: 2019-08-05  11:19
 * @Description:
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserRepository userRepository;
    /**
     * 查找全部
     *
     * @return
     */
    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    /**
     * 通过用户名查找
     *
     * @param userName
     * @return
     */
    @Override
    public List<User>  findUserByUserName(String userName) {
        return userRepository.findByName(userName);
    }

    /**
     * 通过年龄删除用户
     *
     * @param age
     */
    @Override
    @Transactional
    public void deleteUserByAge(Integer age) {
        userRepository.deleteUserByAge(age);
    }


    @Override
    @Transactional
    public void deleteUserByAgeBetween(Integer min,Integer max){
        userRepository.deleteUserByAgeBetween(min,max);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<User> findByPage(final Integer pageNum, final Integer pageSize) {
        Pageable pageable=new Pageable() {
            @Override
            public int getPageNumber() {
                return pageNum;
            }

            @Override
            public int getPageSize() {
                return pageSize;
            }

            @Override
            public int getOffset() {
                return (pageNum-1)*pageSize;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public Pageable next() {
                return null;
            }

            @Override
            public Pageable previousOrFirst() {
                return null;
            }

            @Override
            public Pageable first() {
                return null;
            }

            @Override
            public boolean hasPrevious() {
                return true;
            }
        };

        return userRepository.findAll(pageable);
    }

    /**
     * 添加并f返回实体
     *
     * @param user
     * @return
     */
    @Override
    @Transactional
    public User saveAndFulsh(User user) {
        return userRepository.saveAndFlush(user);
    }

    /**
     * 返回表中数据条数
     *
     * @return
     */
    @Override
    public long count() {
        return userRepository.count();
    }
}
